import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";

/**
 * @date: 2018-08-01

 */
import { getResponse, createPagination } from 'utils/utils';
import { getPublicKey as _getPublicKey } from 'services/api';
import { fetchSMSList as _fetchSMSList, fetchServerType as _fetchServerType, createSMS as _createSMS, editSMS as _editSMS, deleteSMS as _deleteSMS } from "../services/smsConfigService";
export default {
  namespace: 'smsConfig',
  state: {
    smsData: {},
    // 查询数据列表
    serverTypeList: [],
    // 服务类型
    pagination: {},
    // 分页器
    publicKey: '' // 密码公钥

  },
  effects: {
    // 获取短信数据
    fetchSMSList: /*#__PURE__*/_regeneratorRuntime.mark(function fetchSMSList(_ref, _ref2) {
      var payload, call, put, res, list;
      return _regeneratorRuntime.wrap(function fetchSMSList$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              payload = _ref.payload;
              call = _ref2.call, put = _ref2.put;
              _context.next = 4;
              return call(_fetchSMSList, payload);

            case 4:
              res = _context.sent;
              list = getResponse(res);

              if (!list) {
                _context.next = 9;
                break;
              }

              _context.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  smsData: list,
                  pagination: createPagination(list)
                }
              });

            case 9:
            case "end":
              return _context.stop();
          }
        }
      }, fetchSMSList);
    }),
    // 获取服务类型
    fetchServerType: /*#__PURE__*/_regeneratorRuntime.mark(function fetchServerType(_, _ref3) {
      var call, put, response, list;
      return _regeneratorRuntime.wrap(function fetchServerType$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              call = _ref3.call, put = _ref3.put;
              _context2.next = 3;
              return call(_fetchServerType);

            case 3:
              response = _context2.sent;
              list = getResponse(response);

              if (!list) {
                _context2.next = 8;
                break;
              }

              _context2.next = 8;
              return put({
                type: 'updateState',
                payload: {
                  serverTypeList: list
                }
              });

            case 8:
            case "end":
              return _context2.stop();
          }
        }
      }, fetchServerType);
    }),
    // 新建保存
    createSMS: /*#__PURE__*/_regeneratorRuntime.mark(function createSMS(_ref4, _ref5) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function createSMS$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref4.payload;
              call = _ref5.call;
              _context3.next = 4;
              return call(_createSMS, _objectSpread({}, payload));

            case 4:
              result = _context3.sent;
              return _context3.abrupt("return", getResponse(result));

            case 6:
            case "end":
              return _context3.stop();
          }
        }
      }, createSMS);
    }),
    // 编辑保存
    editSMS: /*#__PURE__*/_regeneratorRuntime.mark(function editSMS(_ref6, _ref7) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function editSMS$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref6.payload;
              call = _ref7.call;
              _context4.next = 4;
              return call(_editSMS, _objectSpread({}, payload));

            case 4:
              result = _context4.sent;
              return _context4.abrupt("return", getResponse(result));

            case 6:
            case "end":
              return _context4.stop();
          }
        }
      }, editSMS);
    }),
    deleteSMS: /*#__PURE__*/_regeneratorRuntime.mark(function deleteSMS(_ref8, _ref9) {
      var payload, call, result;
      return _regeneratorRuntime.wrap(function deleteSMS$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              payload = _ref8.payload;
              call = _ref9.call;
              _context5.next = 4;
              return call(_deleteSMS, _objectSpread({}, payload));

            case 4:
              result = _context5.sent;
              return _context5.abrupt("return", getResponse(result));

            case 6:
            case "end":
              return _context5.stop();
          }
        }
      }, deleteSMS);
    }),
    getPublicKey: /*#__PURE__*/_regeneratorRuntime.mark(function getPublicKey(_, _ref10) {
      var call, put, res;
      return _regeneratorRuntime.wrap(function getPublicKey$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              call = _ref10.call, put = _ref10.put;
              _context6.next = 3;
              return call(_getPublicKey);

            case 3:
              res = _context6.sent;

              if (!res) {
                _context6.next = 7;
                break;
              }

              _context6.next = 7;
              return put({
                type: 'updateState',
                payload: {
                  publicKey: res.publicKey
                }
              });

            case 7:
              return _context6.abrupt("return", res);

            case 8:
            case "end":
              return _context6.stop();
          }
        }
      }, getPublicKey);
    })
  },
  reducers: {
    updateState: function updateState(state, action) {
      return _objectSpread(_objectSpread({}, state), action.payload);
    }
  }
};